package com.study.book.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.study.book.common.R;
import com.study.book.mapper.LibraryMapper;
import com.study.book.pojo.Collect;
import com.study.book.pojo.Library;
import com.study.book.service.CollectService;
import com.study.book.mapper.CollectMapper;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author ASUS
* @description 针对表【Collect】的数据库操作Service实现
* @createDate 2024-05-05 18:18:07
*/
@Service
public class CollectServiceImpl extends ServiceImpl<CollectMapper, Collect>
    implements CollectService{

    @Autowired
    private CollectMapper collectMapper;

    @Autowired
    private LibraryMapper libraryMapper;

    @Override
    public R<List<Library>> listCollect(String id) {
        if (Strings.isBlank(id)){
            return R.error("参数异常");
        }
        List<Collect> collects = collectMapper.selectList(new LambdaQueryWrapper<Collect>().eq(Collect::getAdminid, id).select(Collect::getLibraryid));
        if (collects.size()==0)return R.ok();
        List<Library> libraries = libraryMapper.selectList(new LambdaQueryWrapper<Library>().in(Library::getId, collects.stream().map(Collect::getLibraryid).collect(Collectors.toList())).orderByDesc(Library::getRecommend));
        return R.ok(libraries);
    }
}




